/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.repository.mapper.project;

import cn.ac.nercis.pes.common.utils.ConvertUtils;
import cn.ac.nercis.pes.common.constant.project.SecurityLevel;
import cn.ac.nercis.pes.common.constant.project.SystemState;
import cn.ac.nercis.pes.model.dto.project.SystemDTO;
import cn.ac.nercis.pes.model.dto.project.SystemDetailDTO;
import cn.ac.nercis.pes.model.dto.project.SystemEvaluationDTO;
import cn.ac.nercis.pes.model.dto.project.SystemProcessDTO;
import cn.ac.nercis.pes.repository.model.project.ProjectDO;
import cn.ac.nercis.pes.repository.model.project.SystemDO;
import cn.ac.nercis.pes.repository.model.project.SystemEvaluationDO;
import cn.ac.nercis.pes.repository.model.project.SystemProcessDO;
import lombok.NonNull;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

import java.util.Objects;
import java.util.Optional;

/**
 * 系统信息类型映射
 *
 * @author zpy
 * @version 1.0.0
 */
@Mapper(componentModel = "spring",
        imports = {Optional.class,
                Objects.class,
                SystemState.class,
                SecurityLevel.class,
                ProjectDO.class,
                ConvertUtils.class},
        unmappedTargetPolicy = ReportingPolicy.IGNORE,
        builder = @Builder(disableBuilder = true))
public interface SystemMapper {
    SystemMapper INSTANCE = Mappers.getMapper(SystemMapper.class);

    @Mapping(target = "state",expression = "java(Optional.ofNullable(entity).map(SystemDO::getState).map(SystemState::getName).orElse(\"\"))")
    @Mapping(target = "securityLevel",expression = "java(Optional.ofNullable(entity).map(SystemDO::getSecurityLevel).map(SecurityLevel::getName).orElse(\"\"))")
    @Mapping(target = "createTime",expression = "java(Optional.ofNullable(entity).map(o->ConvertUtils.toString(o.getCreateTime())).orElse(\"\"))")
    @Mapping(target = "projectId",expression = "java(Optional.ofNullable(entity.getProject()).map(ProjectDO::getId).orElse(\"\"))")
    @Mapping(target = "projectName",expression = "java(Optional.ofNullable(entity.getProject()).map(ProjectDO::getName).orElse(\"\"))")
    SystemDTO toDTO(SystemDO entity);

    SystemDO toDO(SystemDetailDTO dto);

    SystemEvaluationDTO toEvaluationDTO(SystemEvaluationDO entity);
    SystemEvaluationDO toEvaluationDO(SystemEvaluationDTO dto);

    SystemProcessDTO toSystemProcessDTO(SystemProcessDO entity);
    SystemProcessDO toSystemProcessDO(SystemProcessDTO dto);

    /**
     * 转换为被测系统详情信息
     * @param entity 被测系统信息
     * @param evaluationDO 被测系统实体
     * @return 返回被测系统详情信息
     */
    default SystemDetailDTO toDetailDTO(@NonNull SystemDO entity,SystemEvaluationDO evaluationDO){
        if(Objects.isNull(evaluationDO)){
            evaluationDO = new SystemEvaluationDO();
        }
        return SystemDetailDTO.builder()
                .id(entity.getId())
                .name(entity.getName())
                .securityLevel(entity.getSecurityLevel())
                .sLevel(entity.getSLevel())
                .aLevel(entity.getALevel())
                .gLevel(entity.getGLevel())
                .reqLevel(entity.getReqLevel())
                .filingNumber(entity.getFilingNumber())
                .desc(entity.getDesc())
                .projectId(entity.getProject().getId())
                .systemEvaluation(toEvaluationDTO(evaluationDO))
                .build();
    }

}